﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Notepad_Tremens
{
    public partial class MainForm : Form
    {
        public string backupPath = Application.UserAppDataPath;
        public bool ChangedContent = false;
        public int backupinterval=10000;
        public string filename2="Untitled";
        public MainForm(string[] args)
        {
            InitializeComponent();
            if (args.Length >= 2)
            {
                OpenFile(args[1].ToString());
                ChangedContent = false;
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(Application.ExecutablePath);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            StatusLabel.Text = "Wälkômme wälkômme du me";
            this.Text = "Untitled - Notepad Tremens";
            checkConfig();
        }

        private void checkConfig()
        {
            if (!File.Exists(backupPath + "\\BackupInterval.ini"))
            {
                try
                {
                    File.WriteAllText((backupPath + "\\BackupInterval.ini"), "10000");
                }
                catch { StatusLabel.Text = "Error creating ini file"; }
            }
            else
            {
                try
                {
                    backupinterval = int.Parse(File.ReadAllText(backupPath + "\\BackupInterval.ini"));
                }
                catch { StatusLabel.Text = "Error loading ini file"; }
            }
            timer.Interval = backupinterval;
        }

        private void backup()
        {
            try
            {
                string savedate;
                savedate = DateTime.Now + ":" + DateTime.Now.Millisecond.ToString();
                savedate = savedate.Replace(":", ".");
                savedate = savedate.Replace("\\", ".");
                File.WriteAllText(backupPath + "\\" + "Backup - " + savedate + ".txt", textBox.Text);
                StatusLabel.Text = savedate + ": Backup saved!";
            }
            catch (Exception e)
            {
                MessageBox.Show("Backup error...\n", e.ToString());
            }
            
            //ChangedContent=false;
        }
        private void OpenFile(string path)
        {
            try
            {
                filename2 = path.ToString();
                this.Text = filename2.ToString();
                textBox.Text = File.ReadAllText(path);
                ChangedContent = false;
            }
            catch (Exception e)
            {
                MessageBox.Show("ERROR OPENING FILE:  \n" + e.ToString());
            }

        }
        private void SaveFile(string path, string content) {
            try
            {
                File.WriteAllText(path, content);
                ChangedContent = false;
            }
            catch (Exception e)
            {
               MessageBox.Show("ERROR SAVING FILE:  \n" + e.ToString());
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            timer.Start();
            ChangedContent = true;
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            backup();
            timer.Stop();
        }

        private void showBackupsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", Application.UserAppDataPath);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                OpenFile(fd.FileName);
                //this.Text = fd.f + " - " + Application.ProductName;
            }

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveAs();
        }

        private void saveAs()
        {
            try
            {
                SaveFileDialog sf = new SaveFileDialog();
                sf.OverwritePrompt = true;
                sf.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    SaveFile(sf.FileName, textBox.Text);
                    this.Text = sf.FileName + " - " + Application.ProductName;
                    filename2 = sf.FileName;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error while trying to save as...\n", e.ToString());
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.Paste();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox.SelectAll();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ChangedContent == true)
            {
                DialogResult dlg = MessageBox.Show("UNSAVED CHANGES! Save before quit?", "Unsaved changes", MessageBoxButtons.YesNoCancel);
                if (dlg == DialogResult.Yes)
                {
                   save();
                }
                else if (dlg == DialogResult.No)
                {
                }
                else if (dlg == DialogResult.Cancel)
                {
                    e.Cancel=true;
                }

            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox ab = new AboutBox();
            ab.ShowDialog();
        }

        private void interToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(timer.Interval.ToString());
        }

        private void homepageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://notepadtremens.codeplex.com/");
        }

        private void documentationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://notepadtremens.codeplex.com/documentation");
        }

        private void reportAnIssueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://notepadtremens.codeplex.com/workitem/list/basic");
        }


        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            save();
        }

        private void save()
        {
            if (ChangedContent == true)
            {
                if (filename2 != "Untitled")
                {
                    SaveFile(filename2, textBox.Text);
                    StatusLabel.Text = "Saved: " + filename2;
                }
                else { saveAs(); }
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            changeinterval chinterval = new changeinterval(backupPath);
            switch (chinterval.ShowDialog())
            {
                case DialogResult.OK:
                    checkConfig();
                    StatusLabel.Text = "Backup interval changed";
                    break;
                case DialogResult.Cancel:
                    break;
            }
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

       
    }
}
